package com.example.logisticsingle.authorization;


import com.example.logisticsingle.String.StringUtils;
import com.example.logisticsingle.requsest.RequestUtils;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.Assert;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
 * Processes an authentication form submission. Called {@code AuthenticationProcessingFilter} prior to Spring Security
 *
 */
public class LoginFilter extends AbstractAuthenticationProcessingFilter {
    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";
    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";
    public static final String SPRING_SECURITY_FROM_PLATFORM_KEY = "platform";
    public static final String USERNAME_LOGINID_SPLIT = "-";

    private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
    private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
    private String platformParameter = SPRING_SECURITY_FROM_PLATFORM_KEY;
    private boolean postOnly = true;

    protected LoginFilter(String defaultFilterProcessesUrl) {
        super(defaultFilterProcessesUrl);
    }

    public LoginFilter() {
        super(new AntPathRequestMatcher("/authentication/form", "POST"));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException, IOException, ServletException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

        Map<String, Object> params = RequestUtils.getParamsFormRequest(request);
        String username = params.get(SPRING_SECURITY_FORM_USERNAME_KEY).toString();
        String password = params.get(SPRING_SECURITY_FORM_PASSWORD_KEY).toString();
        String platform = params.get(SPRING_SECURITY_FROM_PLATFORM_KEY).toString();
        username = StringUtils.isNotNull(username)?username.trim():"";
        password = StringUtils.isNotNull(password)?password.trim():"";
        platform = StringUtils.isNotNull(platform)?platform.trim():"PC";
        String loginName = StringUtils.isNotNull(platform)?platform+USERNAME_LOGINID_SPLIT+username:username;
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(loginName, password);
        setDetails(request, authRequest);
        return getAuthenticationManager().authenticate(authRequest);
    }

    protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

    public void setUsernameParameter(String usernameParameter) {
        Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
        this.usernameParameter = usernameParameter;
    }

    public void setPasswordParameter(String passwordParameter) {
        Assert.hasText(passwordParameter, "Password parameter must not be empty or null");
        this.passwordParameter = passwordParameter;
    }

    public boolean isPostOnly() {
        return postOnly;
    }

    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

    public String getUsernameParameter() {
        return usernameParameter;
    }

    public String getPasswordParameter() {
        return passwordParameter;
    }

    public String getPlatformParameter() {
        return platformParameter;
    }

    public void setPlatformParameter(String platformParameter) {
        this.platformParameter = platformParameter;
    }

}
